package com.cxlin.cwanandroid.ui.home.fragment

import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.cxlin.base.BaseFragment
import com.cxlin.base.ext.binding
import com.cxlin.base.ext.dp2px
import com.cxlin.base.ext.toast
import com.cxlin.base.mvi.ext.collectFLow
import com.cxlin.cwanandroid.R
import com.cxlin.cwanandroid.databinding.FragmentNavigationBinding
import com.cxlin.cwanandroid.ui.home.adapter.NavigationAdapter
import com.cxlin.cwanandroid.ui.home.adapter.VerticalTabAdapter
import com.cxlin.cwanandroid.ui.home.viewmodel.NavigationViewModel
import com.cxlin.cwanandroid.ui.login.viewmodel.LoginViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import q.rorbin.verticaltablayout.VerticalTabLayout
import q.rorbin.verticaltablayout.widget.TabView

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/5/6 10:12
 */
class NavigationFragment : BaseFragment(){

    private val binding by binding<FragmentNavigationBinding>()
    private val viewModel by viewModel<NavigationViewModel>()
    private val navigationAdapter by lazy { NavigationAdapter() }
    override fun getLayoutId() = R.layout.fragment_navigation

    override fun initView() {
        initTabLayout()
    }

    private fun initTabLayout(){
        binding.tabLayout.addOnTabSelectedListener(object : VerticalTabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabView?, position: Int) {
                scrollToPosition(position)
            }

            override fun onTabReselected(tab: TabView?, position: Int) {

            }

        })
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = navigationAdapter
        }


    }

    private fun scrollToPosition(position:Int){
        val layoutManager = binding.recyclerView.layoutManager as LinearLayoutManager
        val firstPosition = layoutManager.findFirstVisibleItemPosition()
        val lastPosition = layoutManager.findLastVisibleItemPosition()
        when {
            position <= firstPosition || position >= lastPosition -> binding.recyclerView.smoothScrollToPosition(position)
            else -> binding.recyclerView.run {
                smoothScrollBy(0, this.getChildAt(position - firstPosition).top - this.dp2px(8))
            }
        }
    }

    override fun initData() {
        viewModel.sendIntent(NavigationViewModel.NavigationUiIntent.GetNavigationList())
    }

    override fun observeStateAndEvent() {
        viewModel.uiStateFlow.collectFLow(this){
            when(this){
                is NavigationViewModel.NavigationUiState.Init ->{
                }
                is NavigationViewModel.NavigationUiState.FetchNavigationsSuccess ->{
                    this.navigations?.let {
                        binding.tabLayout.setTabAdapter(VerticalTabAdapter(this.navigations.map { it.name }))
                        navigationAdapter.submitList(this.navigations)
                    }

                }
            }
        }
        viewModel.effectFlow.collectFLow(this) {
            when (this) {
                is LoginViewModel.LoginEvent.ToastEvent -> {
                    requireActivity().toast(
                        this.message, if (this.short) Toast.LENGTH_SHORT else Toast.LENGTH_LONG
                    )
                }
            }
        }
    }

}